package pri.damai.xiaowu.goods.server.api.rpc;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;
import pri.damai.xiaowu.common.core.form.IdForm;
import pri.damai.xiaowu.common.core.util.ExceptionUtil;
import pri.damai.xiaowu.common.core.vo.ResultVO;
import pri.damai.xiaowu.goods.client.SkuService;
import pri.damai.xiaowu.goods.common.command.SkuCreateCommand;
import pri.damai.xiaowu.goods.common.command.StockReduceCmd;
import pri.damai.xiaowu.goods.common.vo.GoodsInfoVO;
import pri.damai.xiaowu.goods.server.entity.ProductSku;
import pri.damai.xiaowu.goods.server.service.impl.ProductSkuServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *
 * @description
 * @author YangXinFu
 * @date 14:55 2022/7/14
 **/
@RestController
public class GoodsFeignClient implements SkuService {
    @Resource
    ProductSkuServiceImpl skuService;

    @Override
    public ResultVO<Void> createSku(SkuCreateCommand skuCreateCommand) {

        ProductSku productSku = new ProductSku();
        BeanUtils.copyProperties(skuCreateCommand, productSku);

        skuService.save(productSku);

        return ResultVO.success();
    }

    @Override
    public ResultVO<Void> reduceSku(StockReduceCmd stockReduceCmd) {
        skuService.reduceSkuStock(stockReduceCmd);
        return ResultVO.success();
    }

    @Override
    public ResultVO<Void> reduceSkus(List<StockReduceCmd> stockReduceCmds) {
        skuService.reduceSkuStock(stockReduceCmds);
        return ResultVO.success();
    }

    @Override
    public ResultVO<GoodsInfoVO> getSkuById(IdForm idForm) {
        ProductSku productSku = skuService.getById(idForm.getId());
        ExceptionUtil.isNull(productSku,"商品不存在。");
        GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
        BeanUtils.copyProperties(productSku, goodsInfoVO);
        return ResultVO.success(goodsInfoVO);
    }
}
